import UE from '../utils/error'

export default {
  install(Vue) {

    /**
     * 发送ajax请求
     * @param { Object } options
     * @tip   options.ctx指向当前VueComponent实例上下文
     */
    Vue.prototype.$ajax = function(options = UE.fnArgumentsEmptyErr('Axios ajax', 'options')) {
      UE.fixedTypeEmptyErr(options, 'object')
      options.ctx = !options.ctx ? this : UE.fixedTypeEmptyErr(options.ctx, 'object')
      options.headers = !options.headers ? { 'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest' } : UE.fixedTypeEmptyErr(options.headers, 'object')
      return new Promise((resolve, reject) => {
        options.ctx.$axios({
          method: !options.method ? 'get' : options.method,
          url: UE.fixedTypeEmptyErr(options.url, 'string'),
          data: !options.data ? null : options.data,
          headers: options.headers
        }).then(res => {
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    Vue.prototype.$ajaxPost = function(url, data = UE.fnArgumentsEmptyErr('Axios ajaxPost', 'data'), params={}) {
      return new Promise((resolve, reject) => {
        this.$axios({
          method: 'post',
          headers: {'token': localStorage.getItem('token')},
          url: UE.fixedTypeEmptyErr(url, 'string'),
          params,
          data: data
        }).then(res => {
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    Vue.prototype.$ajaxGet = function(url, params={}) {
      return new Promise((resolve, reject) => {
        this.$axios({
          method: 'get',
          headers: {'token': localStorage.getItem('token')},
          url: UE.fixedTypeEmptyErr(url, 'string'),
          params
        }).then(res => {
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 屏幕宽高
    Vue.prototype.$getViewportSize = function(){
      return {
        width: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,//兼容性获取屏幕宽度
        height: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight//兼容性获取屏幕高度
      }
    },
    Vue.prototype.$AS = {
      success: '000000'
    },
    Vue.prototype.$channelNo = 'YN001'
  }
}